//
//  XLFileListViewCell.m
//  XLConversationInputView
//
//  Created by ilongge on 2022/8/9.
//

#import "XLFileListViewCell.h"
#import "XLCIViewResourcesManager.h"
#import "XLConversationInputViewDef.h"
#import "XLFileListModel.h"

@interface XLFileListViewCell()
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subTitleLabel;
@property (nonatomic, strong) UIImageView *selectIconImageView;
@property (nonatomic, strong) UIButton *moreButton;
@property (nonatomic, copy) void (^moreButtonBlock)(void);
@end

@implementation XLFileListViewCell


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.selectIconImageView];
        [self.contentView addSubview:self.iconImageView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.subTitleLabel];
        [self.contentView addSubview:self.moreButton];
    }
    return self;
}

- (void)setFileAttrModel:(XLFileListModel *)fileAttrModel{
    
    self.iconImageView.image = [XLCIViewResourcesManager imageNamed:fileAttrModel.logo];
    self.titleLabel.text = [NSString stringWithFormat:@"%@.%@", fileAttrModel.fileName, fileAttrModel.fileExtension];
    float sum = fileAttrModel.fileSize.floatValue;
    NSInteger index = 0;
    while (sum > 1024) {
        sum = sum / 1024.0;
        index ++;
    }
    NSString *danwei = @"";
    switch (index) {
        case 0:
        {
            danwei = @"B";
        }
            break;
        case 1:
        {
            danwei = @"KB";
        }
            break;
        case 2:
        {
            danwei = @"MB";
        }
            break;
        case 3:
        {
            danwei = @"GB";
        }
            break;
        default:
            break;
    }
    self.subTitleLabel.text = [NSString stringWithFormat:@"%@   %d %@", fileAttrModel.fileCreationDate, (int)sum, danwei];
    if (index >= 2 && sum > 50.0) {
        fileAttrModel.isSelect = NO;
        self.selectIconImageView.image = nil;
    }
    else{
        if (fileAttrModel.isSelect) {
            self.selectIconImageView.image = [XLCIViewResourcesManager imageNamed:@"icon_选中"];
        }
        else{
            self.selectIconImageView.image = [XLCIViewResourcesManager imageNamed:@"icon_未选中"];
        }
    }
}

- (void)moreButtonBlock:(void (^)(void))block {
    _moreButtonBlock = block;
}

- (void)emojiIconButtonAction {
    if (self.moreButtonBlock){
        self.moreButtonBlock();
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat width = self.frame.size.width;
    self.selectIconImageView.frame = CGRectMake(10, 20, 20, 20);
    self.iconImageView.frame = CGRectMake(CGRectGetMaxX(self.selectIconImageView.frame) + 5, 10, 40, 40);
    self.moreButton.frame = CGRectMake(width - 30, 20, 20, 20);
    self.titleLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImageView.frame)+5,
                                       10,
                                       CGRectGetMinX(self.moreButton.frame) - CGRectGetMaxX(self.iconImageView.frame) - 10,
                                       20);
    self.subTitleLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImageView.frame)+5,
                                          30,
                                          CGRectGetMinX(self.moreButton.frame) - CGRectGetMaxX(self.iconImageView.frame) - 10,
                                          20);
}

- (UIImageView *)iconImageView {
    if (_iconImageView == nil) {
        _iconImageView = [[UIImageView alloc] init];
        
    }
    return _iconImageView;
}

- (UIImageView *)selectIconImageView {
    if (_selectIconImageView == nil) {
        _selectIconImageView = [[UIImageView alloc] init];
    }
    return _selectIconImageView;
}

- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont fontWithName:kXLCIPingFangSC_Regular size:14];
        _titleLabel.textColor = [UIColor blackColor];
    }
    return _titleLabel;
}

- (UILabel *)subTitleLabel {
    if (_subTitleLabel == nil) {
        _subTitleLabel = [[UILabel alloc] init];
        _subTitleLabel.font = [UIFont fontWithName:kXLCIPingFangSC_Regular size:10];
        _subTitleLabel.textColor = [UIColor blackColor];
    }
    return _subTitleLabel;
}

- (UIButton *)moreButton {
    if (_moreButton == nil) {
        _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moreButton setImage:[XLCIViewResourcesManager imageNamed:@"icon_more"] forState:UIControlStateNormal];
        [_moreButton addTarget:self action:@selector(emojiIconButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreButton;
}

@end
